package com.bf.electroplating.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bf.electroplating.login.UserContext;
import com.bf.electroplating.login.UserDetail;
import com.bf.electroplating.mapper.ParkContingencyTeamMapper;
import com.bf.electroplating.mapper.ParkContingencyTeamPersonMapper;
import com.bf.electroplating.pojo.dto.contingency.ParkContingencyTeamDTO;
import com.bf.electroplating.pojo.dto.contingency.ParkContingencyTeamPersonDTO;
import com.bf.electroplating.pojo.dto.contingency.ParkContingencyTeamQueryDTO;
import com.bf.electroplating.pojo.entity.ComCompany;
import com.bf.electroplating.pojo.entity.ParkContingencyTeam;
import com.bf.electroplating.pojo.entity.ParkContingencyTeamPerson;
import com.bf.electroplating.pojo.vo.ResultVO;
import com.bf.electroplating.pojo.vo.contingency.ParkContingencyTeamVO;
import com.bf.electroplating.service.ComCompanyService;
import com.bf.electroplating.service.ParkContingencyTeamService;
import jakarta.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author 15451
 * @description 针对表【park_contingency_team(园区应急队伍表)】的数据库操作Service实现
 * @createDate 2025-06-18 14:31:54
 */
@Service
public class ParkContingencyTeamServiceImpl extends ServiceImpl<ParkContingencyTeamMapper, ParkContingencyTeam>
        implements ParkContingencyTeamService {

    @Resource
    private ParkContingencyTeamPersonMapper teamPersonMapper;
    @Resource
    private ComCompanyService comCompanyService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(ParkContingencyTeamDTO dto) {
        ParkContingencyTeam team = new ParkContingencyTeam();
        BeanUtils.copyProperties(dto, team);
        save(team);
        savePerson(dto, team);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(ParkContingencyTeamDTO dto) {
        ParkContingencyTeam team = new ParkContingencyTeam();
        BeanUtils.copyProperties(dto, team);
        team.setId(dto.getTid());
        updateById(team);
        teamPersonMapper.delete(new LambdaQueryWrapper<ParkContingencyTeamPerson>()
                .eq(ParkContingencyTeamPerson::getTeamId, team.getId()));
        savePerson(dto, team);
    }

    private void savePerson(ParkContingencyTeamDTO dto, ParkContingencyTeam team) {
        if (dto.getTeamPersons() != null && !dto.getTeamPersons().isEmpty()) {
            for (ParkContingencyTeamPersonDTO personDTO : dto.getTeamPersons()) {
                ParkContingencyTeamPerson person = new ParkContingencyTeamPerson();
                BeanUtils.copyProperties(personDTO, person);
                person.setId(null);
                person.setTeamId(team.getId());
                teamPersonMapper.insert(person);
            }
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(String id) {
        teamPersonMapper.deleteById(id);
    }

    @Override
    public Page<ParkContingencyTeamVO> page(ParkContingencyTeamQueryDTO dto) {
        UserDetail user = UserContext.getUser();
        Page<ParkContingencyTeamVO> parkContingencyTeamVOPage= baseMapper.getTeamPage(
                new Page<>(dto.getPageNum(), dto.getPageSize()),
                StringUtils.trimToNull(dto.getTeamName()), StringUtils.trimToNull(dto.getCompanyId()),
                user
        );
        parkContingencyTeamVOPage.getRecords().forEach(parkContingencyTeamVO -> {
            ComCompany company=comCompanyService.getById(parkContingencyTeamVO.getCompanyId());
            if(company!=null){
                parkContingencyTeamVO.setCompanyName(company.getCompanyName());
            }
        });
        return parkContingencyTeamVOPage;
    }

    @Override
    public ResultVO<ParkContingencyTeamDTO> getDetail(String id) {
        ParkContingencyTeam team = getById(id);
        if (team != null) {
            ParkContingencyTeamDTO dto = new ParkContingencyTeamDTO();
            BeanUtils.copyProperties(team, dto);
            dto.setTid(team.getId());

            List<ParkContingencyTeamPersonDTO> persons = teamPersonMapper.selectList(new LambdaQueryWrapper<ParkContingencyTeamPerson>()
                            .eq(ParkContingencyTeamPerson::getTeamId, id))
                    .stream()
                    .map(person -> {
                        ParkContingencyTeamPersonDTO personDTO = new ParkContingencyTeamPersonDTO();
                        BeanUtils.copyProperties(person, personDTO);
                        personDTO.setTeamId(person.getTeamId());
                        return personDTO;
                    })
                    .toList();
            dto.setTeamPersons(persons);
            return ResultVO.ok(dto);
        }
        return ResultVO.ok(null);
    }

    @Override
    public Boolean batchDelete(List<String> ids) {
        teamPersonMapper.deleteByIds(ids);
        return null;
    }
}




